package com.ohos.connect.devicecloud.auth.modular.login.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ohos.connect.devicecloud.auth.modular.login.model.dto.AuthAppPwdDTO;
import com.ohos.connect.devicecloud.auth.modular.login.model.dto.AuthPhoneValidCodeLoginDTO;
import com.ohos.connect.devicecloud.auth.modular.login.model.dto.PhoneValidCodeQueryDTO;
import com.ohos.connect.devicecloud.auth.modular.login.model.dto.ValidPhoneCodeDTO;
import com.ohos.connect.devicecloud.auth.modular.login.model.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ohos.connect.devicecloud.auth.core.annotation.SaClientCheckLogin;
import com.ohos.connect.devicecloud.auth.core.enums.SaClientTypeEnum;
import com.ohos.connect.devicecloud.auth.core.pojo.SaBaseClientLoginUser;
import com.ohos.connect.devicecloud.auth.modular.login.result.AuthPicValidCodeResult;
import com.ohos.connect.devicecloud.auth.modular.login.service.AuthService;
import com.ohos.connect.devicecloud.common.pojo.CommonResult;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * APP登录控制器
 */
@Api(tags = "APP登录控制器")
@ApiSupport(author = "OHOS_CONNECT_TEAM", order = 1)
@RestController
@RequestMapping(value = "/api/v1/device_cloud/auth")
@Validated
public class AuthAppController {

    @Resource
    private AuthService authService;

    /**
     * APP获取图片验证码
     **/
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "APP获取图片验证码",hidden = true)
    @GetMapping("/app/getPicCaptcha")
    public CommonResult<AuthPicValidCodeResult> getPicCaptcha() {
        return CommonResult.data(authService.getPicCaptcha(SaClientTypeEnum.APP.getValue()));
    }

    /**
     * APP根据图片验证码获取手机验证码
     **/
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "APP根据图片验证码获取手机验证码",hidden = true)
    @GetMapping("/app/getPhoneValidCodeByPicCaptcha")
    public CommonResult<String> getPhoneValidCodeByPicCaptcha(@Valid AuthGetPhoneValidCodeParam authGetPhoneValidCodeParam) {
        return CommonResult.data(authService.getPhoneValidCodeByPicCaptcha(authGetPhoneValidCodeParam, SaClientTypeEnum.APP.getValue()));
    }

    /**
     * APP获取手机验证码
     **/
    @ApiOperationSupport(order = 2)
    @ApiOperation("APP获取手机验证码")
    @GetMapping("/app/getPhoneValidCode")
    public CommonResult<ValidCodeReqNoVO> getPhoneValidCode(@Valid PhoneValidCodeQueryDTO phoneValidCodeQueryDTO) {
        return CommonResult.data(authService.getPhoneValidCode(phoneValidCodeQueryDTO, SaClientTypeEnum.APP.getValue()));
    }

    /**
     * APP获取邮箱验证码
     **/
    @ApiOperationSupport(order = 2)
    @ApiOperation("APP获取邮箱验证码")
    @GetMapping("/app/getEmailValidCode")
    public CommonResult<ValidCodeReqNoVO> getEmailValidCode(@NotBlank(message = "邮箱不能为空") String email) {
        return CommonResult.data(authService.getEmailValidCode(email, SaClientTypeEnum.APP.getValue()));
    }

    /**
     * 校验手机验证码
     **/
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "校验手机验证码",hidden = true)
    @GetMapping("/app/validPhoneCode")
    public CommonResult validPhoneCode(@Valid ValidPhoneCodeDTO validPhoneCodeDTO) {
        authService.validPhoneCode(validPhoneCodeDTO);
        return CommonResult.ok();
    }

    /**
     * APP账号密码登录
     **/
    @ApiOperationSupport(order = 3)
    @ApiOperation("APP账号密码登录")
    @PostMapping("/app/doLogin")
    public CommonResult<AuthAppLoginVO> doLogin(@RequestBody @Valid AuthAppPwdDTO authAppPwdDTO) {
        return CommonResult.data(authService.doLogin(authAppPwdDTO, SaClientTypeEnum.APP.getValue()));
    }

    /**
     * APP手机验证码登录
     **/
    @ApiOperationSupport(order = 4)
    @ApiOperation("APP手机验证码登录")
    @PostMapping("/app/doLoginByPhone")
    public CommonResult<String> doLoginByPhone(@RequestBody @Valid AuthPhoneValidCodeLoginDTO authPhoneValidCodeLoginDTO) {
        return CommonResult.data(authService.doLoginByPhone(authPhoneValidCodeLoginDTO, SaClientTypeEnum.APP.getValue()));
    }

    /**
     * APP退出
     **/
    @ApiOperationSupport(order = 5)
    @ApiOperation("APP退出")
    @SaClientCheckLogin
    @GetMapping("/app/doLogout")
    public CommonResult<String> doLogout(@NotBlank(message ="设备id不能为空") String device) {
        authService.doLogout(device);
        return CommonResult.ok();
    }

    /**
     * APP获取用户信息
     *
     **/
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "APP获取用户信息",hidden = true)
    @SaClientCheckLogin
    @GetMapping("/app/getLoginUser")
    public CommonResult<SaBaseClientLoginUser> getLoginUser() {
        return CommonResult.data(authService.getClientLoginUser());
    }
}
